using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Drawing;
using System.Drawing.Drawing2D;

using System.IO;
using System.Drawing.Imaging;
using System.Collections.Generic;


namespace ASP_TF2LogParser.UserControls
{
    public partial class MapPlotControl : System.Web.UI.UserControl
    {
        protected void Page_Load( object sender, EventArgs e )
        {

        }

        public void PlotMap( int logId )
        {
            List<string> selectedClasses = (List<string>) Session["SelectedClasses"];
            List<string> selectedPlayers = (List<string>) Session["SelectedPlayers"];

            bool restrictByClass = true;
            bool restrictByPlayers = true;

            if ( selectedClasses.Count == 0 )
                restrictByClass = false;

            if ( selectedPlayers.Count == 0 )
                restrictByPlayers = false;



            List<LogParser.Kill> killList = new List<LogParser.Kill>();

            killList = LogParser.DA.KillsStatsModel.KillsSelect( logId );

            LogParser.DA.DataObjects.LogFilesDS.LogFilesRow lfr = LogParser.DA.LogFileModel.GetLog( logId );

            // get map info
            LogParser.DA.DataObjects.MapsDS.MapsRow mapRow = LogParser.DA.MapModel.GetMap( lfr.MapId );



            string mapURL = Server.MapPath( "MapImages/" + mapRow.MapImageUrl + ".jpg" );
            System.Drawing.Image img = System.Drawing.Image.FromFile( mapURL );
            Bitmap b = new Bitmap( img );
            Graphics g = Graphics.FromImage( b );
            g.SmoothingMode = SmoothingMode.HighQuality;




            int xOffset = -1 * mapRow.MinX;
            int yOffset = -1 * mapRow.MinY;

            bool flipX = mapRow.FlipX;
            bool flipY = mapRow.FlipY;

            int yMax = mapRow.MaxY + yOffset;
            int xMax = mapRow.MaxX + xOffset;


            double scaleRatioX = (double) ( ( (double) xMax ) / ( (double) mapRow.ResX ) );
            double scaleRatioY = (double) ( ( (double) yMax ) / ( (double) mapRow.ResY ) );

            foreach ( LogParser.Kill kill in killList )
            {
                if ( selectedClasses.Contains( kill.AttackerClass ) || restrictByClass == false )
                {
                    if ( selectedPlayers.Contains( kill.Attacker.PlayerId ) || restrictByPlayers == false )
                    {

                        Point pointToPlot = new Point( Convert.ToInt32( kill.VictimPosition.X + xOffset ), Convert.ToInt32( kill.VictimPosition.Y + yOffset ) );

                        if ( flipY )
                        {
                            pointToPlot.Y = yMax - pointToPlot.Y;
                        }

                        Brush fillColor;

                        if ( kill.VictimTeam.ToUpper() == "RED" )
                        {
                            fillColor = Brushes.Red;
                        }
                        else
                        {
                            fillColor = Brushes.Blue;
                        }

                        g.FillEllipse( fillColor, Convert.ToInt32( ( (Double) pointToPlot.X / scaleRatioX ) ), Convert.ToInt32( ( (Double) pointToPlot.Y / scaleRatioY ) ), 6, 6 );
                    }
                }
            }


            MemoryStream tempStream = new MemoryStream();
            b.Save( tempStream, ImageFormat.Png );
            Response.ClearContent();
            Response.ContentType = "image/png";
            Response.BinaryWrite( tempStream.ToArray() );
            Response.Flush();

            b.Dispose();
            b = null;
            g.Dispose();
            g = null;


        }
    }
}